

* Input:
* Data/accinvent_invoice_panel.dta
* Data/solar_accounts.dta
* Data/Addresses_SA3.dta
* Data/postcode_month_PVinstall.dta
* Data/solar_firstinvoice.dta

* Output:
* Data/accounts_inventory_panel_PV.dta

clear


********************************************************************
******************** INVENTORY AND ACCOUNT DATA TO SOLAR ***********
********************************************************************

use Data/accinvent_invoice_panel.dta

merge m:1 account_id using Data/solar_accounts.dta

	gen first_month = mofd(first_date)
	format first_month %tm

	gen solar_start_month = mofd(solar_start_date)
	format solar_start_month %tm

	rename _merge merge_solaraccounts

* merge postcode PV installation on contract start month
	rename contract_start_month month_year

merge m:1 account_id using Data/Addresses_SA3.dta, keepusing(postcode SA3) 

	rename _merge merge_address

merge m:1 postcode month_year using Data/postcode_month_PVinstall.dta 

	drop if _merge ==2

	rename _merge merge_PVpenetration

	rename month_year contract_start_month
	sort account_id contract_start_month

merge m:1 account_id using Data/solar_firstinvoice.dta 

	rename _merge merge_invoice


	order account_id contract_start_month contract_start contract_end contract_end_month date_end first_date first_month first_invoice first_fitinvoice solar_start_date solar_start_month FIT_program


save Data/accounts_inventory_panel_PV.dta, replace
